package com.intel.factorytest.activity;

import java.io.IOException;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.util.Log;
import android.view.View;

import com.intel.factorytest.R;

public class EarpieceKhz extends BaseActivity {
    private static final String TAG = "EarpieceKhz";
    private MediaPlayer mediaPlayer;
    AudioManager audioManager;
    private static int save_volume_music = 30;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.receiver_test);
        super.initView();

        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
        audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        // audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
        //audioManager.setMode(AudioManager.MODE_IN_CALL);
        // mediaPlayer = MediaPlayer.create(this, R.raw.test);
        //audioManager.setSpeakerphoneOn(false);
        save_volume_music = audioManager
                .getStreamVolume(AudioManager.STREAM_VOICE_CALL);

        audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, 100, 20);
        Uri tempUri =  Uri.parse("android.resource://com.intel.factorytest/"
                + R.raw.test_1khz);
        try {
            mediaPlayer.setDataSource(EarpieceKhz.this, tempUri);
            mediaPlayer.setLooping(true);
            mediaPlayer.prepare();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        mediaPlayer.start();
        mediaPlayer.setVolume(1f, 1f);
        
        //playMP3();

    }

    public Uri getDefaultRingtoneUri(int type) {
        return RingtoneManager.getActualDefaultRingtoneUri(this, type);
    }


    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume");
        mItemTestFinish = true;//add by wangsm
    }

    @Override
    protected void onDestroy() {
        Log.d(TAG, " receiver test onDestroy");
        try {
            if (mediaPlayer != null) {
                mediaPlayer.release();
            }
        } catch (NullPointerException e) {
            e.printStackTrace();
        }

        recoverMode();
        mediaPlayer = null;
        super.onDestroy();
    }

    @Override
    public void finish() {
        Log.d(TAG, " receiver test finish ");
        try {
            if (mediaPlayer != null) {
                mediaPlayer.release();
            }
        } catch (NullPointerException e) {
            e.printStackTrace();
        }

        setTestResult();
        super.finish();
    }

    @Override
    protected void onPause() {
        Log.d(TAG, "onPause");
        super.onPause();
/*        try {
            if (mediaPlayer != null) {
                mediaPlayer.release();
            }
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
        mediaPlayer = null;*/
    }

    private void playMP3() {
        if (mediaPlayer.isPlaying()) {
            mediaPlayer.reset();// 重置为初始状态
        }
        try {

            mediaPlayer.prepare();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }// 缓冲
        mediaPlayer.start();// 开始或恢复播放
        mediaPlayer.setLooping(true);
        mediaPlayer
                .setOnCompletionListener(new MediaPlayer.OnCompletionListener() {// 播出完毕事件
                    @Override
                    public void onCompletion(MediaPlayer arg0) {
                        mediaPlayer.stop();
                        mediaPlayer.release();
                    }
                });
        mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {// 错误处理事件
                    @Override
                    public boolean onError(MediaPlayer player, int arg1,
                            int arg2) {
                        mediaPlayer.release();
                        return false;
                    }
                });

    }

    private void recoverMode() {
        Log.d(TAG, "handsetReceiver recoverMode begin");
        //audioManager.setSpeakerphoneOn(true);
        audioManager.setMode(AudioManager.MODE_NORMAL);

        audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL,
                save_volume_music, AudioManager.FLAG_PLAY_SOUND);
        //Log.d(TAG, "handsetReceiver recoverMode end");
    }
}
